<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="邮件名称" prop="project">
        <el-input v-model="queryParams.project" placeholder="请输入邮件名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收件人" prop="recipientsName">
        <el-input v-model="queryParams.recipientsName" placeholder="请输入收件人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" filterable>
          <el-option v-for="dict in typeOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="draftsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50"></el-table-column>
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="邮件名称" align="center" prop="project" />
      <el-table-column label="收件人" align="center" prop="recipientsName" />
      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <!-- <dict-tag :options="dict.type.email_table_type" :value="scope.row.type" /> -->
          <span v-if="scope.row.type == 0">普通文件</span>
          <span v-else>机密文件</span>
        </template>
      </el-table-column>
      <el-table-column label="发件时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)"
            v-hasPermi="['email:drafts:edit']">
            编辑
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['email:drafts:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getDraftsList" />
  </div>
</template>

<script>
import { selectDraftsList, selectDraftsByConditionList, updateSymbol } from "@/api/email/drafts";

export default {
  name: "Drafts",
  dicts: ['email_table_status', 'email_table_type'],
  data() {
    return {
      initialQueryParams: {
        pageNum: 1,
        pageSize: 10,
        project: null,
        recipientsName: null,
        type: '',
      },
      // 类型选择
      typeOptions: [
        { value: 0, label: '普通文件' },
        { value: 1, label: '机密文件' },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 草稿箱表格数据
      draftsList: [
        {
          // id: '',
          project: '',
          recipientsName: '',
          deptName: '',
          type: '',
          status: '',
          createTime: ''
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        project: '',
        recipientsName: '',
        type: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  activated() {
    this.queryParams = { ...this.initialQueryParams };
    this.getDraftsList();
  },
  created() {
    this.getDraftsList();
  },
  methods: {

    handleEdit(row) {
      // if (!this.$hasPermi(['email:drafts:edit'])) {
      //   // 权限检查失败，可以在这里处理，比如显示错误消息  
      //   return;
      // }
      // 权限检查通过，进行路由跳转  
      // 假设您有一个名为 `editEmail` 的路由，并且它需要接受一个 `id` 参数  
      // this.$router.push({ name: 'editEmail', params: { id: row.id } });
      // 或者如果是查询参数（通常用于动态路由匹配）  
      this.$router.push({ path: '/email/write', query: row })
    },


    // 根据条件查询当前用户的草稿箱列表
    getConditionDraftsList() {
      this.loading = true;
      selectDraftsByConditionList(this.queryParams).then(response => {
        this.draftsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 查询当前用户的草稿箱列表
    getDraftsList() {
      this.loading = true;
      selectDraftsList(this.queryParams).then((response) => {
        this.draftsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getConditionDraftsList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.getDraftsList();
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.getDraftsList();
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认删除邮箱发件箱编号为"' + id + '"的数据项？').then(() => {
        return updateSymbol(id);
      }).then(() => {
        this.getDraftsList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        this.$modal.msgError("删除失败");
      });
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

  }
};
</script>
